<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\JsExpression;
use common\models\farmerhouse\FarmerhouseHouse;
use kartik\file\FileInput;
use common\models\business\BusinessAdress;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $model common\models\farmerhouse\FarmerhouseHouse */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <div class="adv-table editable-table ">
                <div class="farmerhouse-house-form">
                    <?php $form = ActiveForm::begin([
                        'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                        'fieldConfig' => [
                            'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                        ]
                    ]); ?>



                        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'personid')->widget(\kartik\select2\Select2::className(), [
                            'options' => ['placeholder' => '请输入业务员真实姓名 ...'],
                            'initValueText'=>!$model->person?'':$model->person->name,
                            'pluginOptions' => [
                                'allowClear' => true,
                                'minimumInputLength' => 1,//重要
                                'language' => [
                                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                ],
                                'ajax' => [
                                    'url' => '/business/search/search-farmer',
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'cache' => true
                                ],
                                'width' => '400px',
                                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                            ],
                        ]);
                        ?>

                        <?= $form->field($model, 'farmerid')->widget(\kartik\select2\Select2::className(), [
                            'options' => ['placeholder' => '请输入农户真实姓名 ...'],
                            'initValueText'=>!$model->farmer?'':$model->farmer->name,
                            'pluginOptions' => [
                                'allowClear' => true,
                                'minimumInputLength' => 1,//重要
                                'language' => [
                                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                ],
                                'ajax' => [
                                    'url' => '/business/search/search-farmer',
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'cache' => true
                                ],
                                'width' => '400px',
                                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                            ],
                        ]);
                        ?>

                        <?= $form->field($model, 'roomnum')->textInput() ?>

                        <?= $form->field($model, 'canfeed')->dropDownList(FarmerhouseHouse::$canfeed) ?>

                        <?= $form->field($model, 'isrecommand')->dropDownList(FarmerhouseHouse::$isrecommand) ?>

                        <?= $form->field($model, 'status')->dropDownList(FarmerhouseHouse::$statues) ?>


                        <?= $form->field($model, 'caninnum')->textInput() ?>

                        <?= $form->field($model, 'price')->textInput(['maxlength' => true]) ?>


                        <?= $form->field($model, 'envphoto[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->envphoto,true,'envphoto'),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'envphoto',FarmerhouseHouse::tableName(),$model->id),
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                            ],
                        ]); ?>


                        <?= $form->field($model, 'accommodationphoto[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->accommodationphoto,true,'accommodationphoto'),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'accommodationphoto',FarmerhouseHouse::tableName(),$model->id),
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                            ],
                        ]); ?>

                        <?= $form->field($model, 'city')->dropDownList(ArrayHelper::map(BusinessAdress::allCity(),'id','name')); ?>

                        <?= $form->field($model, 'county')->dropDownList(ArrayHelper::map(BusinessAdress::allCounty(),'id','name')); ?>

                        <?= $form->field($model, 'town')->dropDownList(ArrayHelper::map(BusinessAdress::allTown(),'id','name')); ?>

                        <?= $form->field($model, 'address')->textInput(['maxlength' => true]) ?>


                        <?= $form->field($model, 'accommodation')->textarea() ?>

                        <?= $form->field($model, 'envdesc')->textarea() ?>

                        <?= $form->field($model,'detail')->widget('kucha\ueditor\UEditor',[
                            'clientOptions' => [
                                //编辑区域大小
                                'initialFrameHeight' => '350',
                                //设置语言
                                'lang' =>'zh-cn', //中文为 zh-cn
                                //定制菜单
                                'toolbars' => [
                                    [
                                        'fullscreen', 'source', 'undo', 'redo', '|',
                                        'fontsize',
                                        'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat',
                                        'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
                                        'forecolor', 'backcolor', '|',
                                        'lineheight', '|',
                                        'indent', '|' ,  'map',
                                        'snapscreen','insertimage'
                                    ],
                                ]
                            ]
                        ]); ?>

                        <?= $form->field($model,'feedlive')->widget('kucha\ueditor\UEditor',[
                            'clientOptions' => [
                                //编辑区域大小
                                'initialFrameHeight' => '350',
                                //设置语言
                                'lang' =>'zh-cn', //中文为 zh-cn
                                //定制菜单
                                'toolbars' => [
                                    [
                                        'fullscreen', 'source', 'undo', 'redo', '|',
                                        'fontsize',
                                        'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat',
                                        'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
                                        'forecolor', 'backcolor', '|',
                                        'lineheight', '|',
                                        'indent', '|',
                                        'snapscreen','insertimage'
                                    ],
                                ]
                            ]
                        ]); ?>

                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
                    </div>
                    <?php ActiveForm::end(); ?>
                </div>
            </div>
        </div>
    </section>
</div>
</div>

<script>
    <?php $this->beginBlock('js_end') ?>

    //排序
    $('.field-farmerhousehouse-accommodationphoto').on('filesorted', function(event, params) {
        var data = [];
        $.each(params.stack,function(k,v){
            data.push({'path':v.caption,'table':v.extra.table,'field':v.extra.field,'id':v.extra.id,'idname':v.extra.idname});
        })
        $.post('/site/sortfile',{'data':data},function(res){
            console.log(res)
        });
        console.log('File sorted ', params.previewId, params.oldIndex, params.newIndex, params.stack);
    });

    $('.field-farmerhousehouse-envphoto').on('filesorted', function(event, params) {
        var data = [];
        $.each(params.stack,function(k,v){
            data.push({'path':v.caption,'table':v.extra.table,'field':v.extra.field,'id':v.extra.id,'idname':v.extra.idname});
        })
        $.post('/site/sortfile',{'data':data},function(res){
            console.log(res)
        });
        console.log('File sorted ', params.previewId, params.oldIndex, params.newIndex, params.stack);
    });

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>